﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Autofac;
using ConceptsFramework.Containers.Global;
using ILCodeExplorer;
using PatternExplorer.DerivedContainers;

namespace PatternExplorer
{
    class Program
    {
        public static List<AssemblyExplorer> AllClasses; 
        private ContainerBuilder appContainer = new ContainerBuilder();

        static void Main(string[] args)
        {
            // appContainer.RegisterType()
            AllClasses = new List<AssemblyExplorer>();
            if (args.Length == 0)
            {
                Console.WriteLine("Usage: folder to explore; string to avoid in namespaces (lowercase and uppercase matter, separated with spaces)");
            }
            else
            {
                string[] filePaths = Directory.GetFiles(args[0]);
                List<string> namespaceAvoidance = new List<string>(args.Skip(1));
                Globals.NamespaceAvoidancePragmas = namespaceAvoidance;
                List<string> pathsToExplore = new List<string>();
                foreach(string aFilePath in filePaths)
                {
                    bool exploreDll = true;
                    foreach (string strAvoidance in namespaceAvoidance)
                    {
                        int lastSlashIndex = aFilePath.LastIndexOf('\\');
                        string strReducedPath = aFilePath.Substring(lastSlashIndex + 1);
                        if ((strReducedPath.StartsWith(strAvoidance)))
                            exploreDll = false;
                    }
                    if (exploreDll&aFilePath.ToLower().EndsWith(".dll"))
                    {
                        pathsToExplore.Add(aFilePath);
                    }
                }

                int assemblyCount = 1;
                foreach(string strFilePath in pathsToExplore)
                {
                    Console.WriteLine("Inspecting assembly " + assemblyCount + " of "+pathsToExplore.Count+", "+strFilePath);
                    Assembly anAssembly = Assembly.LoadFrom(strFilePath);
                    AssemblyExplorer aAssemblyExplorer = new AssemblyExplorer(anAssembly);
                    AllClasses.Add(aAssemblyExplorer);
                    assemblyCount++;
                }
            }

            var types = TypeSummarySingletonContainer.Instance;
            var inheritanceTree = TypeSummarySingletonContainer.Instance.InheritanceTrees;
            TypeSummarySingletonContainer.Instance.BuildInheritenceRelationships();
            TypeSummarySingletonContainer.Instance.BuildCallTreeRelationships();
            
            var globalConceptContainer = GlobalConceptsSingletonContainer.Instance;
            var globalRelationshipsContainer = GlobalRelationshipsSingletonContainer.Instance;

            Console.WriteLine("Code exploration is done");
            Console.WriteLine("Number of types "+ types.Container.Count);
            Console.ReadLine();
            
        }
    }
}
